<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/7/31 0031
// +----------------------------------------------------------------------
// | Description:QueryList 采集
// +----------------------------------------------------------------------

namespace app\admin\controller;
header('Content-Type: text/html; charset=utf-8');

use app\common\controller\AdminBaseController;
use QL\QueryList;


class Qlist extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        /*
        * $t = mb_convert_encoding($t,'ISO-8859-1','utf-8');
          $t = mb_convert_encoding($t,'utf-8','GBK');
        */
        //http://querylist.cc/docs/guide/v4/scraper-single   手册地址
    }


    public function index(){
        // 待采集的页面地址
        $url = 'https://www.cnbeta.com/';
        // 采集规则
        $rules = [
            // 文章标题
            'title' => ['dl>dt>a','text'],
            // 文章链接地址
            'link' => ['dl>dt>a','href'],
            // 文章缩略图
            'img' => ['dl>a>img','src'],
            // 文章简介
            'summary' => ['dl>dd>p','text'],
            //分类
            'type' =>['.meta-data>label>a','text'],

            'type_url' =>['.meta-data>label>a','href'],

            'desc' =>['.meta-data>ul>li','text','-li',function($d){
                if (!empty($d)){
                    $d=explode("&nbsp;&nbsp;",$d);
                }
                return $d;
            }],
        ];

        // 切片选择器
        $range = '.items-area>.item';

        // $data = QueryList::get($url)->rules($rules)->query()->range($range)->getData(); //单个数组
        //dump($data->all());

        $data=QueryList::get($url)->rules($rules)->query()->range($range)->queryData();//多个数组
        dump($data);

        // $data=QueryList::get($url)->rules($rules)->encoding('UTF-8','GB2312')->query()->range($range)->getData();

    }

}